﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

internal class CommandSet:IDisposable
{
	private readonly SqlParameter bid;
	private readonly SqlParameter tid;
	private readonly SqlParameter lid;
	private readonly SqlParameter cstr;
	private readonly SqlParameter sid;
	private readonly SqlParameter val;


	public CommandSet(SqlCommand Command, SqlString StationId, SqlString Caster, SqlString TagValue)
	{
		bid = new SqlParameter();
		bid.DbType = DbType.Int64;
		bid.ParameterName = "@bid";

		tid = new SqlParameter();
		tid.DbType = DbType.Int64;
		tid.ParameterName = "@tid";

		lid = new SqlParameter();
		lid.DbType = DbType.Int64;
		lid.ParameterName = "@lid";

		cstr = new SqlParameter();
		cstr.ParameterName = "@cstr";
		cstr.DbType = DbType.String;

		sid = new SqlParameter();
		sid.ParameterName = "@sid";
		sid.DbType = DbType.String;

		val = new SqlParameter();
		val.ParameterName = "@val";
		val.DbType = DbType.String;

		this.Command = Command;

		this.Command.Parameters.Add(bid);
		this.Command.Parameters.Add(tid);
		this.Command.Parameters.Add(lid);
		this.Command.Parameters.Add(cstr);
		this.Command.Parameters.Add(sid);
		this.Command.Parameters.Add(val);

		GetLiveInfoId(StationId, Caster);

		if (!TagValue.IsNull)
		{
			GetTagsId(TagValue);
		}
	}

	private void GetLiveInfoId(SqlString StationId, SqlString Caster)
	{
		sid.Value = StationId;
		cstr.Value = Caster;

		this.Command.CommandText = "EXECUTE dbo.RegistLiveInfo @sid,@cstr;";
		lid.Value = this.Command.ExecuteScalar();
	}

	public void GetTagsId(SqlString TagValue)
	{
		this.Command.CommandText = "EXECUTE dbo.RegistTag @val";
		val.Value = TagValue;
		tid.Value = this.Command.ExecuteScalar();
	}

	public string CommandText
	{
		get
		{
			return this.Command.CommandText;
		}
		set
		{
			this.Command.CommandText = value;
		}
	}

	public object ExecuteScalar()
	{
		return this.Command.ExecuteScalar();
	}

	public int ExecuteNonQuery()
	{
		return this.Command.ExecuteNonQuery();
	}


	public SqlCommand Command
	{
		get;
		private set;
	}

	public long LiveInfoId
	{
		get
		{
			return (long)lid.Value;
		}
	}

	public long BindId
	{
		get
		{
			return (long)bid.Value;
		}
	}

	public long TagsId
	{
		get
		{
			return (long)tid.Value;
		}
	}

	public SqlString StationId
	{
		get
		{
			return (SqlString)sid.Value;
		}
		set
		{
			sid.Value = value;
		}
	}

	public SqlString Caster
	{
		get
		{
			return (SqlString)cstr.Value;
		}
		set
		{
			cstr.Value = value;
		}
	}

	public SqlString TagValue
	{
		get
		{
			return (SqlString)val.Value;
		}
		set
		{
			val.Value = value;
		}
	}

	#region IDisposable メンバ

	public void Dispose()
	{
		Command.Dispose();
	}
	#endregion
}